package com.nanjia.boot.product.category.controller;


import com.nanjia.boot.common.model.Option;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.product.category.model.form.ProductCategoryForm;
import com.nanjia.boot.product.category.model.query.ProductCategoryQuery;
import com.nanjia.boot.product.category.model.vo.ProductCategoryVO;
import com.nanjia.boot.product.category.service.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类前端控制层
 *
 * @author 周洋
 * @since 2025-07-04 15:24
 */
@Tag(name = "商品分类接口")
@RestController
@RequestMapping("/api/v1/product-category")
@RequiredArgsConstructor
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;

    @Operation(summary = "商品分类分页列表")
    @GetMapping("/page")
    public Result<List<ProductCategoryVO>> list(ProductCategoryQuery queryParams) {

        return Result.success(productCategoryService.getProductCategoryList(queryParams));
    }

    @Operation(summary = "部门下拉列表")
    @GetMapping("/options")

    public Result<List<Option<Long>>> listProductCategoryOptions() {
        List<Option<Long>> list = productCategoryService.listProductCategoryOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增商品分类")
    @PostMapping
    public Result<Void> saveProductCategory(@RequestBody @Valid ProductCategoryForm formData,HttpServletRequest request) {
        boolean result = productCategoryService.saveProductCategory(formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取商品分类表单数据")
    @GetMapping("/{id}/form")
    public Result<ProductCategoryForm> getProductCategoryForm(
            @Parameter(description = "商品分类ID") @PathVariable Long id
    ) {
        ProductCategoryForm formData = productCategoryService.getProductCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品分类")
    @PutMapping(value = "/{id}")
    public Result<Void> updateProductCategory(
            @Parameter(description = "商品分类ID") @PathVariable Long id,
            @RequestBody @Validated ProductCategoryForm formData,HttpServletRequest request
    ) {
        boolean result = productCategoryService.updateProductCategory(id, formData,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除商品分类")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteProductCategorys(
            @Parameter(description = "商品分类ID，多个以英文逗号(,)分割") @PathVariable String ids, HttpServletRequest request
            ) {
        boolean result = productCategoryService.deleteProductCategorys(ids, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }
}
